package wicketcool.common.testutils;

import wicketcool.common.Const;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

/**
 * User: rabbit
 * Date: Sep 7, 2009
 */
public class SystemOutMock {

    private PrintStream oldOutput;
    private ByteArrayOutputStream bytes;


    public void mockSystemOut() {
        bytes = new ByteArrayOutputStream();
        oldOutput = System.out;
        System.setOut(new PrintStream(bytes));
    }


    public void unmockSystemOut() {
        System.setOut(oldOutput);
    }

    public String getSystemOut() {
        return bytes.toString();
    }

    public String getSystemOut(int index) {

        Scanner scanner = new Scanner(bytes.toString());
        scanner.useDelimiter(Const.NEWLINE_SPARATOR);
        int count = 0;
        String result = null;
        while (scanner.hasNext()) {
            result = scanner.next();
            if (count == index) {
                break;
            }
            count++;
        }
        return result;
    }

}
